#include <stdio.h>
#include "smsh.h"

/**
  * The process(char **arglist) function is called by the main loop
  * It sits in front of the execute() function. This layer handles
  * two main calsses of processing:
  * a) built-in functions (e.g. exit(), set, = , read, ..)
  * b) control structures (e.g. if, while, for)
  */

int is_control_command(char *);
int do_control_command(char **);
int ok_to_execute();

/**
  * purpose: process user command
  * returns: result of processing command
  * details: if a built-in then call appropriate function, 
  * if not execute()
  * errors: arise form subroutines, handled there
  */
int process(char **args) {
	int rv = 0;
	if (args[0] == NULL) {
		rv = 0;
	} else if (is_control_command(args[0])) {// check if the command is control statement
		rv = do_control_command(args);
	} else if (ok_to_execute()) {		// check if the commmand is executable
		// first check if it is a built-in command
		if (!builtin_command(args, &rv)) {
			rv = execute(args);
		}
	}
	return rv;
}
